<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AdjustModel
 *
 * @author 敏学
 */
class AdjustModel extends AdjustPublicModel {

    //put your code here
    protected $tableName = 'adjust';
    protected $_validDetail = array(
        array("product_id", 'require', 'require', 1),
        array("warehouse_id", 'require', 'require', 1),
        array("color_id", 'require', 'require', 0),
        array("size_id", 'require', 'require', 0),
        array("quantity", 'currency', 'double', 1),
        array("capability", 'z_integer', 'z_integer', 0),
        array("dozen", 'z_integer', 'z_integer', 0),
        array("price", 'double', 'double', 1),
    );


    /**
     * 所有订单列表SQL
     *
     * @return  array
     */
    function indexSql() {

        $count = $this->exists('select 1 from adjust_detail where adjust_id=adjust.id and quantity>0 and ' . getWhere($_POST['detail']))->where(getWhere($_POST['main']))->count();
        $this->setPage($count);
        $ids = $this->field('id')->exists('select 1 from adjust_detail where adjust_id=adjust.id and quantity>0 and ' . getWhere($_POST['detail']))->where(getWhere($_POST['main']))->order('adjust_no desc')->page()->selectIds();
        $info['from'] = 'adjust a LEFT JOIN adjust_detail b ON a.id = b.adjust_id';
        $info['group'] = ' group by a.id order by adjust_no desc';
        $info['where'] = ' where a.id in' . $ids;
        $info['field'] = 'a.id AS id,a.adjust_no AS adjust_no,a.adjust_date AS adjust_date,a.currency_id AS currency_id,b.adjust_id,b.product_id AS product_id,b.size_id AS size_id,b.quantity AS quantity,b.capability AS capability,b.warehouse_id AS warehouse_id,sum(quantity*capability*dozen) AS sum_quantity,sum(quantity) AS total_quantity,sum(quantity*capability*dozen*price) AS money';
        return 'select ' . $info['field'] . ' from ' . $info['from'] . $info['where'] . $info['group'];
    }

}

?>
